import axios from "axios";

export const getExecutingTasksByWorkerID = workerid => {
    // 参数：众包工人id
    return axios.get(
        `/task/worker/working/${workerid}`
    ).then(res => {
        return res.data;
    })
}

// GET
export const getFinishedTasksByWorkerID = workerid => {
    // 参数：众包工人id
    return axios.get(
        `/task/worker/finished/${workerid}`
    ).then(res => {
        return res.data;
    })
}
export const getActivityCount = workerid => {
    return axios.get(
        `/user/getActivityCount/${workerid}`
    ).then(res => {
        return res.data;
    })
}
export const getMyMessage = workerid => {
    return axios.get(
        `/message/allmessage/${workerid}`
    ).then(res => {
        return res.data;
    })
}

export const setRead = (messageId, uid) => {
    return axios.get(
        `/message/setRead/${messageId}/?uid=${uid}`
    ).then(res => {
        return res.data;
    })
}
// POST
// 返回应为布尔值，表示workerid的工人是否领取了taskid的任务
export const whetherClaimedCertainTask = (taskid, workerid) => {
    return axios.get(`/work/getStatus/?taskid=${taskid}&workerid=${workerid}`
    ).then(res => {
        return res.data;
    })
}

// POST
export const claimTask = payload => {
    // 参数：workerid / taskid
    console.log(payload);
    return axios.post('/work/pick', {
        workerid: payload.workerid,
        taskid: payload.taskid
    }).then(res => {
        return res.data;
    })
}

// POST
export const submitReport = payload => {
    // 参数：workerid, taskid, environment, description, steps, screenshot
    return axios.post('/report/submit', {
        workerid: payload.workerid,
        taskid: payload.taskid,
        environment: payload.environment,
        description: payload.description,
        steps: payload.steps
    }).then(res => {
        return res.data;
    })
}
// todo lyl interface should be changed?
// 在前端的修改界面好像拿不到oldReportId
// 他们项目获取报告大多使用taskId+workerId当索引，而不是直接用reportId
// 所以这里可能需要改成：由后端自己利用taskId+workerId，查出对应的oldReportId，再做更新
export const modifyReport = payload =>{
    const {taskid,description,steps,workerid} = payload;
    // 这个oldReportId前端拿不到
    const oldReportId = taskid;
    return axios.post(`report/newVersion/${oldReportId}`,{taskid,description,steps,workerid}).then(res=>{
        return res.data;
    })
}

// GET
export const getReportByTaskIDAndWorkerID = (taskid, workerid) => {
    // 参数：workerid / taskid
    return axios.get(
        `/report/workerdetail/${taskid}?workerid=${workerid}`
        //`https://mock.hmilyld.com/mock/62401d10878b3700170b91d9/similarity/getReportByWorkerIDAndTaskID`
    ).then(res => {
        return res.data;
    })
}

// GET
export const getWorkerFeaturesByWorkerID = workerid => {
    return axios.get(
        `https://mock.hmilyld.com/mock/623acfa7878b3700170b91c3/worker/features`
    ).then(res => {
        return res.data;
    })
}

// GET
export const getRecommendTaskListByWorkerIDAndRuleID = (workerid, ruleid) => {
    return axios.get(
        `/recommend/useRule/?ruleid=${ruleid}&workerid=${workerid}`
    ).then(res => {
        return res.data;
    })
}

// GET
// todo
export const getAllClaimedTasksByWorkerID = workerid => {
    return axios.get(
        // 暂时用历史任务顶一下
        `/task/worker/finished/${workerid}`
    ).then(res => {
        return res.data;
    })
}

// GET
export const deleteReportByWorkerIDAndTaskID = (workerid, taskid) => {
    return axios.get(
        `xxx`
    ).then(res => {
        return res.data;
    })
}


// GET 获取所有工人
// todo: 以便关注
export const getAllWorkers = _ => {
    return axios.get(
        `/user/getWorkers`
    ).then(res => {
        return res.data;
    })
}


// GET 获取为某工人推荐的审查报告列表
// todo: 目前暂时用别的方法顶一下
export const getReviewReportListByWorkerID = workerid => {
    return axios.get(
        `/score/recommendReport/${workerid}`
    ).then(res => {
        return res.data;
    })
}

// GET 获取某工人关注了谁
// todo
export const getFollowingByWorkerID = workerid => {
    return axios.get(
        `/follow/getFollows?userId=${workerid}`
    ).then(res => {
        return res.data;
    })
}

// POST 批量关注
// todo
export const followOtherWorkers = payload => {
    return axios.post(
        `/follow/follow`,
        {
            workerid: payload.workerid,
            workeridList: payload.workeridList
        }
    ).then(res => {
        return res.data;
    })
}

// POST 批量取关
// todo
export const unfollowOtherWorkers = payload => {
    return axios.post(
        `/follow/unfollow`,
        {
            workerid: payload.workerid,
            workeridList: payload.workeridList
        }
    ).then(res => {
        return res.data;
    })
}

export const postUserDevice = (userId, device) => {
    const deviceCopy = {...device};
/*    return new Promise((resolve) => {
        console.log('start saving user device: ')
        console.log(deviceCopy)
        setTimeout(() => {
            console.log('user device saved!')
            deviceCopy.id = 10;
            resolve({
                code: 200,
                msg: 'success',
                data: deviceCopy
            });
        }, 2000)
    })*/
    console.log(device);
      return axios.post(
          `/user/device?userId=${userId}`,
              deviceCopy
      ).then(res => {
        return res.data;
      })
}

export const getUserDevices = (userId) => {
/*    return new Promise((resolve) => {
        console.log('start getting user devices: ');
        console.log(userId);
        setTimeout(() => {
            console.log('user devices got!');
            resolve({
                "code": 200,
                "msg": "string",
                "data": [
                    {
                        "id": 0,
                        "name": "测试设备1",
                        "os": "windows7",
                        "ram": 16,
                        "processorArchitecture": "arm"
                    },
                    {
                        "id": 1,
                        "name": "测试设备2",
                        "os": "windows10",
                        "ram": 8,
                        "processorArchitecture": "x86"
                    },
                    {
                        "id": 2,
                        "name": "测试设备3",
                        "os": "ios10",
                        "ram": 32,
                        "processorArchitecture": "arm"
                    }
                ]
            })
        }, 2000)
    })*/
      return axios.get(
          `/user/myDevices?userId=${userId}`
      ).then(res => {
        return res.data;
      })
}
export const deleteUserDevice = (deviceId) => {
/*    return new Promise((resolve) => {
        console.log('start delete device: ');
        console.log(deviceId);
        setTimeout(() => {
            console.log('delete success');
            resolve({
                "code": 200,
                "msg": "",
                "data": {}
            })
        }, 3000)
    })*/
      return axios.delete(
          `/user/deviceToDrop/${deviceId}`
      ).then(res => {
        return res.data;
      });
}

// lyl
export const loadWorkerInfo = userId =>{
    return axios.get(`/user/workerInfo/${userId}`).then(res=>{
        return res.data;
    })
}

export const getUserLevelHistory = userId => {
    return axios.get(
        '/user/workerLevelHistory',
        {
            params: {userId}
        }
    ).then(res => res.data);
/*    return new Promise((resolve) => {
        console.log('start getting user level history');
        console.log(userId)
        setTimeout(() => {
            console.log('user level history got!');
            resolve({
                "code": 200,
                "msg": "string",
                "data": [
                    {
                        "time": "2022-10-08T08:53:52.030Z",
                        "workerLevel": 0,
                        "duration": [
                            "20"
                        ],
                        "reportNumber": 0,
                        "excellentRate": 0,
                        "dishonestRate": 0
                    },
                    {
                        "time": "2022-10-08T08:53:52.030Z",
                        "workerLevel": 0,
                        "duration": [
                            "20"
                        ],
                        "reportNumber": 0,
                        "excellentRate": 0,
                        "dishonestRate": 0
                    }
                ]
            })

        }, 3000)
    })*/
}

//POST 提现
export const balanceWithdraw = payload =>{
    const {userId, amount} = payload;
    const type = 'withdraw';
    return axios
        .post(`/user/withdraw`,{userId,amount,type})
        .then(res =>{
            return res.data
        })

}
